/*******************************************************************************
Paper: Does the Production Approach to Markup Estimation Match a Stylized Fact?
Description: Produces the data for Table 1 and Figure 1a and 1b
Author: Tove Forsbacka
Last Updated: 15 February 2026
*******************************************************************************/

* Setup and environment control 
clear
set more off
capture log close 
version 18.5

* Define path and load the data
*cd "PROJECT_PATH"
use "bokslut_clean.dta"

* Merge in data on output elasticity from production function estimation
merge m:1 SNI2007_1 using "outputelasticity.dta"
drop _merge

* Gen variable for firms' markups
gen markup = OUTEL * (dNTOMS/(dPERSKOS+OUTEL*dINTINPUT))
label variable markup "Markup

drop OUTEL dNTOMS dPERSKOS dMATANLSU dINTINPUT

* Save data
save "markups_firms.dta", replace

* Winsorize markups
foreach var of varlist market_hairdresser market_driving market_grocery market_taxi {
	winsor2 markup if `var' == 1, cuts(5 95) by(year) 
	rename markup_w markup_`var'
}

gen markup_winsorized = 0
foreach var of varlist market_hairdresser market_driving market_grocery market_taxi {
	replace markup_winsorized = markup_`var' if `var' == 1
	drop markup_`var'
}

drop markup
rename markup_winsorized markup

* Calculate average markups for local markets

save "temp_data2.dta", replace

foreach var of varlist market_hairdresser market_driving market_grocery market_taxi {
	use "temp_data2.dta", clear 
	keep if `var' == 1

bysort Kommunkod year: egen markup_mean = mean(markup)
drop markup

drop if count > 1
drop count
drop if firms==.

save "`var'.dta", replace
 }

use "market_hairdresser", clear

foreach var of varlist market_driving market_grocery market_taxi {
append using "`var'.dta"
}

* Drop the two largest regions (Stockholm and Gothenburg)
drop if stockholm == 1 | goteborg == 1 

* Saving the data
save "markups.dta", replace
